﻿/*  
 *  IIS LogFile Analyser, Copyright (C) 2013. http://www.iis-analyser.com
 *  --
 *  This program is free software, provided "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 * 
 *  File Author: Ian Quigley,    2013-02-23
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SQLite;
using System.Data;

namespace DataStore
{
    public class Store 
    {
        private string _filePath;
        private SQLiteConnection _conn;
        private static bool _tablesInitialised = false;       

        public Store(string dbFilePath)
        {
            _filePath = dbFilePath;
            Database.DbFile = dbFilePath;
            Database.DataSource = "Data Source=" + dbFilePath;

            if (!_tablesInitialised)
            {
                FileRecord.InitialiseTable();
                SiteRecord.InitialiseTable();
                VisitRecord.InitialiseTables();
                AccountRecord.InitialiseTable();
                ViewRecord.InitialiseTable();
                _tablesInitialised = true;
            }
        }

        
        public IEnumerable<SiteRecord> Sites
        {
            get
            {
                return SiteRecord.ReadAll();
            }
        }

        public FileRecord FindOrCreateFile( SiteRecord site, string filePath )
        {
            return FileRecord.FindOrCreate( site, filePath );
        }

        public SiteRecord FindOrCreateSite(string siteName, string folder = "")
        {
            return SiteRecord.FindOrCreate( siteName, folder );
        }

        public VisitRecord CreateVisitRecord(FileRecord file, SiteRecord site)
        {
            return new VisitRecord() { FileId = file.Id, SiteId = site.Id };
        }
        
        public void DeleteSite( int id )
        {
            SiteRecord.Delete( id );
        }

        public DataSet SQL(string sql)
        {
            
            DataSet ds = null;
            using (var db = Database.Open())
            using (var command = new SQLiteCommand( sql, db.Connection ))
            {
                db.Connection.Open();
                ds = new DataSet();
                (new SQLiteDataAdapter( command )).Fill( ds );
                db.Connection.Close();
            }

            return ds;
        }

        public SiteRecord SiteById( long siteId )
        {
            return SiteRecord.Read( siteId );
        }

        public AccountRecord AccountLogin( string email, string password)
        {
            if (!AccountRecord.AnyDefined())
            {
                var newGuy = new AccountRecord();
                newGuy.Email = email;
                newGuy.Password = password;
                newGuy.Username = email;
                newGuy.LoginAttempts = 0;
                newGuy.Save();
                return newGuy;
            }

            return AccountRecord.Login( email, password );
        }

        public bool AccountAny()
        {
            return AccountRecord.AnyDefined();
        }

        public ViewRecord View( long id )
        {
            return ViewRecord.Find( id );
        }

        public List<ViewRecord> Views(bool onlyInMenu = false)
        {
            return ViewRecord.ReadAll(onlyInMenu);
        }

        public void DeleteView( long viewId )
        {
            ViewRecord.Delete( viewId );
        }
    }
}
